package com.example.pattern.draw

import android.app.Application
import android.graphics.Color
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.example.pattern.draw.color.ColorModel
import com.example.pattern.utils.ColorUtils
import com.example.pattern.utils.dp2px

class DrawViewModel(application:Application):AndroidViewModel(application) {
    val mToolType = MutableLiveData(ToolType.None)
    var mLineWidth = MutableLiveData(application.dp2px(1))
    var mPaintColor = MutableLiveData(Color.WHITE)
    val mColorModels = MutableLiveData(emptyList<ColorModel>())
    init {
        loadColors(100)
    }
    fun setType(type: ToolType) {
        mToolType.postValue(type)
    }

    fun setLineWidth(size: Int) {
        mLineWidth.postValue(getApplication<Application>().dp2px(size))
    }

    fun setPaintColor(color: Int) {
        if(color == -1){
            mPaintColor.postValue(Color.WHITE)
        }
        mPaintColor.postValue(color)
    }

    fun loadColors(count: Int) {
        val models = arrayListOf<ColorModel>()
        ColorUtils.getRandomColor(count).forEach {
            models.add(ColorModel(it))
        }
       mColorModels.postValue(models)
    }
}